package com.hjy.frontend;


import com.hjy.common.transport.Package;
import com.hjy.common.transport.Packager;

public class Client {
    private RoundTripper rt;

    public Client(Packager packager) {
        this.rt = new RoundTripper(packager);
    }

    /**
     * 执行语句, 返回后端数据库的响应
     * @param stat
     * @return
     * @throws Exception
     */
    public byte[] execute(byte[] stat) throws Exception {
        // 封装成包
        Package pkg = new Package(stat, null);
        // 发送数据给后端, 同时获取回复
        Package resPkg = rt.roundTrip(pkg);
        if (resPkg.getErr() != null) {
            throw resPkg.getErr();
        }
        return resPkg.getData();
    }

    public void close() {
        try {
            rt.close();
        } catch (Exception e) {
        }
    }

}
